pub mod courier;
pub mod client;
pub mod server;
mod utils;

pub use serde;
pub use tweeny_rpc_macros as macros;

use serde::{Serialize, Deserialize};

pub(crate) type ClientLocalRequestId = u32;

#[derive(Serialize, Deserialize)]
pub(crate) struct RpcCallRequest<ReqType> {
    id: ClientLocalRequestId,
    req: ReqType,
}

#[derive(Serialize, Deserialize)]
pub(crate) struct RpcCancelRequest {
    id: ClientLocalRequestId
}

#[derive(Serialize, Deserialize)]
pub(crate) enum RpcRequest<ReqType> {
    Call(RpcCallRequest<ReqType>),
    Cancel(RpcCancelRequest),
}

#[derive(Serialize, Deserialize)]
pub(crate) enum RpcResponse<RespType> {
    Done {
        req_id: ClientLocalRequestId,
        resp: RespType,
    },
    Errored {
        req_id: ClientLocalRequestId
    }
}

impl<Req> RpcResponse<Req> {
    pub(crate) fn id(&self) -> ClientLocalRequestId {
        match self {
            RpcResponse::Errored { req_id } => *req_id,
            RpcResponse::Done { req_id, .. } => *req_id,
        }
    }
}
